

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../plugins/layui/css/layui.css?v=${.Config.basic.version}" media="all">
    <link rel="stylesheet" href="../plugins/layui/css/style.css?v=${.Config.basic.version}" media="all">
    <link rel="stylesheet" href="../layuiadmin/style/admin.css?v=${.Config.basic.version}" media="all">
    <link rel="stylesheet" href="../plugins/zTree/css/metroStyle/metroStyle.css?v=${.Config.basic.version}">
    <link rel="stylesheet" href="../plugins/zTree/css/metroStyle/ext.css?v=${.Config.basic.version}">
    <link rel="stylesheet" href="//at.alicdn.com/t/c/font_3564966_a6tt4yhnzge.css" media="all">
    <style>
        .layui-upload-drag {
            position: relative;
            padding: 30px;
            border: 1px dashed #e2e2e2;
            background-color: #fff;
            text-align: center;
            cursor: pointer;
            color: #999;
        }
    </style>
</head>
<body>
<div class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list" style="padding: 20px 30px 0 0;">

    <div class="layui-form-item">
        <label class="layui-form-label">类型</label>
        <div class="layui-input-inline">
            <select id="type" name="type" lay-filter="type" lay-verify="" lay-search="">
            </select>
        </div>
    </div>


    <div class="layui-form-item">

        <div class="layui-input-block">

            <div class="layui-upload-drag" id="btn_select">
                <i class="layui-icon"></i>
                <p>点击上传，或将文件拖拽到此处</p>
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">


                <div class="layui-upload-list">
                    <table class="layui-table">
                        <thead>
                        <tr><th>文件名</th>
                            <th>大小</th>
                            <th>状态</th>
                            <th>操作</th>
                        </tr></thead>
                        <tbody id="demoList"></tbody>
                    </table>
                </div>
                <button type="button" class="layui-btn layui-btn-sm layui-btn-normal" id="btn_upload">开始上传</button>

        </div>
    </div>

    <div class="layui-form-item layui-hide">
        <input type="button" lay-submit lay-filter="btn_ok" id="btn_ok" value="">
        <input type="button" lay-submit lay-filter="btn_cancel" id="btn_cancel" value="">
    </div>
</div>
<script src="../plugins/layui/layui.js"></script>
<script src="../plugins/jquery/jquery-3.6.0.js?v=${.Config.basic.version}"></script>
<script>
    var form=null;
    var upload=null;
    var element=null;
    var token = window.localStorage.getItem("token");
    var user_info=JSON.parse(window.localStorage.getItem("user_info"));
    token = 'Bearer ' + token;
    var dictionary=JSON.parse(window.localStorage.getItem("dictionary"));
    layui.config({
        base: '../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index','form','upload','element'], function(){
        //var $ = layui.$;
        form = layui.form;
        upload = layui.upload;
        element=layui.element;

        $.ajax({
            type: 'GET',
            url: '../Api/DeviceDefine',
            data: {},
            beforeSend: function(request) {
                request.setRequestHeader("Authorization", 'Bearer '+window.localStorage.getItem("token"));
            },
            dataType: "json",
            success: function (res) {
                console.info(res)
                if(res.code>=0){
                    device_define = res.data;
                    renderSelect();
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.msg('加载设备类型失败');
            }
        });

        //选完文件后不自动上传
        var demoListView = $('#demoList');
        var uploadListIns =upload.render({
            elem: '#btn_select'
            ,url: '../Api/Firmware/Upload'
            ,auto: false
            ,multiple: true
            ,number:3 //最大同时上传3个
            ,field:"upload-file"
            ,headers: {'Authorization': token}
            ,accept: 'file' //普通文件
            ,size:0 //设置文件最大可允许上传的大小，单位 KB。不支持ie8/9
            ,bindAction: '#btn_upload'
            ,data: {
                type: function(){
                    return $('#type').val();
                }
            }
            ,choose: function(obj){
                var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
                //读取本地文件
                obj.preview(function(index, file, result){
                    var tr = $(['<tr id="upload-'+ index +'">'
                        ,'<td>'+ file.name +'</td>'
                        ,'<td>'+ (file.size/1024).toFixed(1) +'kb</td>'
                        ,'<td>等待上传</td>'
                        ,'<td>'
                        ,'<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'
                        ,'<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'
                        ,'</td>'
                        ,'</tr>'].join(''));

                    //单个重传
                    tr.find('.demo-reload').on('click', function(){
                        obj.upload(index, file);
                    });

                    //删除
                    tr.find('.demo-delete').on('click', function(){
                        delete files[index]; //删除对应的文件
                        tr.remove();
                        uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
                    });

                    demoListView.append(tr);
                });
            }
            ,before: function(obj){ //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
                if ($('#type').val().length==0){
                    layer.alert('未选择类型', {
                        icon: 0
                    });
                    return  false;
                }
            }
            ,progress: function(n){
                var percent = n + '%' //获取进度百分比
                console.info(percent);
                element.progress('demo', percent); //可配合 layui 进度条元素使用
            }
            ,done: function(res, index, upload){
                if(res.code == 0){ //上传成功
                    var tr = demoListView.find('tr#upload-'+ index)
                        ,tds = tr.children();
                    tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');
                    tds.eq(3).html(''); //清空操作
                    return delete this.files[index]; //删除文件队列已经上传成功的文件
                }
                this.error(index, upload);
            }
            ,allDone: function(obj){ //当文件全部被提交后，才触发
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layui.table.reload('table'); //重载表格
                parent.layer.close(index); //再执行关闭
            }
            ,error: function(index, upload){
                var tr = demoListView.find('tr#upload-'+ index)
                    ,tds = tr.children();
                tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
                tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
            }
        });

        //监听提交
        form.on('submit(btn_ok)', function(data){
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            parent.layui.table.reload('table'); //重载表格
            parent.layer.close(index); //再执行关闭
        });
    });

    function renderSelect(){
        //栏舍下拉
        var html="";
        for(var i=0;i<device_define.length;i++){
            var selectd="";
            html+="<option "+selectd+" value=\""+device_define[i].device_type+"\">"+device_define[i].device_desc+"</option>";

        }

        $("select[name='type']").html(html);
        form.render('select');
    }
</script>
<script src="../js/check_screen.js?v=${.Config.basic.version}"></script>

</body>
</html>

